<?php

namespace ExAdmin\ui\component\form\field;

use ExAdmin\ui\component\form\Field;
use ExAdmin\ui\component\form\FormItem;

/**
 * 富文本
 * @method $this disk(string $value) 磁盘
 * @method $this height(int $value) 高度
 * @method $this width(int $value) 宽度
 * @method $this options(array $value) tinymce配置选项
 * @method $this tags(array $value) 设置标记元素
 * @method $this toolbar(string $value) 工具栏 undo redo | styleselect | bold italic |alignleft | aligncenter | alignright | bullist | numlist | outdent | indent | removeformat | subscript | superscript | fontsize_formats | cut | copy | paste | forecolor
 */
class Editor extends Field
{
    protected $name = 'ExTinymceEditor';

    public function __construct($field = null, $value = '')
    {
        parent::__construct($field, $value);
        $disk = admin_config('admin.upload.disk');
        $this->disk($disk);
    }

    /**
     * textarea模式
     * @param int $height 高度
     * @return $this
     */
    public function textarea($height = 150)
    {
        $this->height($height);
        $this->options([
            'menubar' => false,
            'toolbar' => false,
            'statusbar' => false,
            'forced_root_block'=>''
        ]);
        return $this;
    }

    public function upload(array $value)
    {
        $upload = $this->attr('upload') ?? [];
        $upload = array_merge($upload, $value);
        $this->attr('upload', $upload);
    }

    public function setFormItem(FormItem $formItem)
    {
        parent::setFormItem($formItem); // TODO: Change the autogenerated stub
        $params = $this->formItem->form()->getCall()['params'];
        $params = array_merge($params,$this->formItem->form()->attr('params') ?? [],['upload_field' => $this->getValidateField(), 'ex_admin_form_action' => 'upload']);
        $this->upload([
            'action' => $this->formItem->form()->attr('url'),
            'params' => $params
        ]);
    }
}
